package com.bsoft.gol.hcs.specialist.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bsoft.gol.hcs.enumcase.*;
import com.bsoft.gol.hcs.followup.dao.FusAfterHosRecordDAO;
import com.bsoft.gol.hcs.satisfaction.dao.PatientSatisfactionSurveyDAO;
import com.bsoft.gol.hcs.specialist.bean.qo.FollowUpRouteQo;
import com.bsoft.gol.hcs.specialist.bean.qo.QueryAfterHosRecordQo;
import com.bsoft.gol.hcs.specialist.bean.qo.UpdateAfterAuditStatusQo;
import com.bsoft.gol.hcs.specialist.bean.qo.UpdateAuditStatusQo;
import com.bsoft.gol.hcs.specialist.bean.request.*;
import com.bsoft.gol.hcs.specialist.bean.response.FollowUpRouteDetailResp;
import com.bsoft.gol.hcs.specialist.bean.response.FollowUpRouteResp;
import com.bsoft.gol.hcs.specialist.bean.response.FusAfterHosRecordDTO;
import com.bsoft.gol.hcs.specialist.bean.vo.FollowUpCountVo;
import com.bsoft.gol.hcs.specialist.bean.vo.FollowUpRecordsVO;
import com.bsoft.gol.hcs.specialist.bean.vo.FollowUpRouteVO;
import com.bsoft.gol.hcs.specialist.dao.*;
import com.bsoft.gol.hcs.specialist.entity.*;
import com.bsoft.gol.hcs.specialist.service.IFollowUpRouteService;
import com.bsoft.gol.hcs.utils.CommonUtils;
import com.bsoft.gol.hcs.utils.ThreadPoolExecutorUtil;
import com.bsoft.gol.hcs.visitrecord.dao.VrInpatientVisitRecordDAO;
import com.bsoft.gol.hcs.visitrecord.dao.VrOutpatientVisitRecordDAO;
import com.bsoft.gol.hcs.visitrecord.entity.VrInpatientVisitRecord;
import com.github.pagehelper.PageInfo;
import ctd.controller.exception.ControllerException;
import ctd.persistence.exception.DAOException;
import ctd.spring.boot.annotation.SsdevReference;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import hcn.base.Organization;
import hcn.base.VrOutpatientVisitRecord;
import hcn.bean.UserAllVo;
import hcn.service.base.IOrganizationService;
import hcn.util.ResultCode;
import org.atag.dao.model.SimplePageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import pcn.service.web.BaseDeviceService;
import service.rpc.BaseDictionaryService;
import service.rpc.IDocTeamRpcService;
import service.rpc.IOrganizationRpcService;

import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @Author:FangZhiyi
 * @Description 随访路径设置
 * @Data Created in 2020/7/21 13:52
 * @Moddified By:
 */
@SsdevService("followUpRoute")
public class FollowUpRouteServiceImpl implements IFollowUpRouteService {

    @Autowired
    FollowUpRouteDAO followUpRouteDAO;
    @Autowired
    FollowUpRecordsDAO followUpRecordsDAO;
    @Autowired
    FollowUpRouteDetailDAO followUpRouteDetailDAO;
    @Autowired
    FollowUpRouteDetailFactorDAO followUpRouteDetailFactorDAO;
    @Autowired
    FollowUpRouteDetailContentDAO followUpRouteDetailContentDAO;
    @Autowired
    FollowUpOperationLogDAO followUpOperationLogDAO;
    @Autowired
    FollowUpRouteDetailFormDAO followUpRouteDetailFormDAO;
    @Autowired
    FollowUpRouteDetailFactorRelationDAO followUpRouteDetailFactorRelationDAO;
    @SsdevReference("hcn.baseDeviceService")
    private BaseDeviceService baseDeviceService;
    @Autowired
    private IOrganizationRpcService organizationRpcService;

    @Autowired
    private FusAfterHosRecordDAO fusAfterHosRecordDAO;

    @Autowired
    private VrInpatientVisitRecordDAO vrInpatientVisitRecordDAO;

    @Autowired
    private VrOutpatientVisitRecordDAO vrOutpatientVisitRecordDAO;

    @Autowired
    private FollowPatientLabelDAO followPatientLabelDAO;

    @Autowired
    private BaseDictionaryService baseDictionaryService;

    @SsdevReference("hcn.organization")
    private IOrganizationService organizationService;

    @Autowired
    PatientSatisfactionSurveyDAO patientSatisfactionSurveyDAO;

    @Autowired
    IDocTeamRpcService docTeamRpcService;

    /**
     * @Author:FangZhiyi
     * @Description 查询随访路径列表
     * @Params [queryFollowUpRouteReq]
     * @Return org.atag.core.base.BaseResponse
     * @Data Created in 2020/7/28 15:04
     * @Moddified By:
     */
    @Override
    @RpcService
    public PageInfo queryFollowUpRouteList(QueryFollowUpRouteReq queryFollowUpRouteReq) throws ControllerException {
        try {
            int count = followUpRouteDAO.queryFollowUpRouteCount(queryFollowUpRouteReq);
            if (count > 0) {
                List<FollowUpRoute> followUpRouteList = followUpRouteDAO.queryFollowUpRouteList(queryFollowUpRouteReq);
                if (!CollectionUtils.isEmpty(followUpRouteList)) {
                    PageInfo pageInfo = new PageInfo(followUpRouteList);
                    pageInfo.setPageNum(queryFollowUpRouteReq.getPageNo());
                    pageInfo.setPageSize(queryFollowUpRouteReq.getPageSize());
                    pageInfo.setTotal(count);
                    return pageInfo;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ControllerException("请求失败" + e.getMessage());
        }
        return new PageInfo();
    }

    /**
     * 根据路径id查询路径基本信息
     *
     * @param follow
     * @return
     * @throws ControllerException
     */
    @Override
    @RpcService
    public FollowUpRouteResp getFollowUpRouteByRouteId(FollowUpBaseReq follow) throws ControllerException {
        FollowUpRouteResp routeResp = new FollowUpRouteResp();
        if (CommonUtils.isEmpty(follow.getRouteId())) {
            throw new ControllerException("路径id不能为空");
        }
        FollowUpRoute followUpRoute = followUpRouteDAO.get(follow.getRouteId());
        BeanUtils.copyProperties(followUpRoute, routeResp);
        if (CommonUtils.isNotEmpty(followUpRoute.getOrganizationId())) {
            Organization organization = organizationRpcService.getOrgDetailByOrgCode(followUpRoute.getOrganizationId());
            routeResp.setOrgId(organization.getOrgId());
        }
        return routeResp;
    }

    /**
     * 更新路径版本信息
     *
     * @param follow
     * @throws ControllerException
     */
    @RpcService
    @Override
    public void updateFollowUpRouteVersion(FollowUpRouteVersionReq follow) throws ControllerException {
        if (CommonUtils.isEmpty(follow.getRouteId())) {
            throw new ControllerException("路径id不能为空");
        }
        UserAllVo allByOnline = baseDeviceService.getAllByOnline();
        FollowUpRoute followUpRoute = new FollowUpRoute();
        followUpRoute.setRouteId(follow.getRouteId());
        if (CommonUtils.isEmpty(follow.getVersionNumber())) {
            followUpRoute.setVersionNumber(new BigDecimal(followUpRoute.getVersionNumber()).add(new BigDecimal("0.1")) + "");
        } else {
            followUpRoute.setVersionNumber(follow.getVersionNumber());
        }
        followUpRoute.setModifyId(allByOnline.getUserId());
        followUpRoute.setModifyUsername(allByOnline.getUserName());
        followUpRoute.setModifyDt(new Date());
        followUpRouteDAO.updateFollowUpRouteVersion(followUpRoute);
        //followUpRouteDAO.updateSelective(followUpRoute);
    }

    /**
     * @Author:FangZhiyi
     * @Description 保存/更新随访路径
     * @Params [saveFollowUpRouteReq]
     * @Return org.atag.core.base.BaseResponse
     * @Data Created in 2020/7/28 15:05
     * @Moddified By:
     */
    @Override
    @RpcService
    public FollowUpRoute saveFollowUpRoute(SaveFollowUpRouteReq saveFollowUpRouteReq) throws ControllerException {
        FollowUpRoute followUpRoute = null;
        byte operationType = 0;
        UserAllVo allByOnline = baseDeviceService.getAllByOnline();
        try {
            followUpRoute = new FollowUpRoute();
            //添加路径租户信息
            followUpRoute.setTenantId(allByOnline.getTenantId());
            if (saveFollowUpRouteReq.getRouteId() == null) {//新增
                BeanUtils.copyProperties(saveFollowUpRouteReq, followUpRoute);
                followUpRoute.setVersionNumber("0.1");
                followUpRoute.setCreatorDt(new Date());
                followUpRoute.setModifyDt(new Date());
                followUpRoute.setModifyUsername(allByOnline.getUserName());
                followUpRouteDAO.save(followUpRoute);
                operationType = 1;
            } else {
                followUpRoute = followUpRouteDAO.get(saveFollowUpRouteReq.getRouteId());
                if (followUpRoute == null) {
                    throw new ControllerException("查无此记录，无法修改，请查证。");
                }
                BeanUtils.copyProperties(saveFollowUpRouteReq, followUpRoute);
                //followUpRoute.setVersionNumber(new BigDecimal(followUpRoute.getVersionNumber()).add(new BigDecimal("0.1")) + "");
                followUpRoute.setModifyDt(new Date());
                followUpRoute.setModifyUsername(allByOnline.getUserName());
                followUpRouteDAO.updateSelective(followUpRoute);
                operationType = 2;
            }
        } catch (BeansException e) {
            e.printStackTrace();
            throw new ControllerException("请求失败" + e.getMessage());
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ControllerException("请求失败" + e.getMessage());
        }
        /*保存操作日志*/
        FollowUpRoute finalFollowUpRoute = followUpRoute;
        byte finalOperationType = operationType;
        ThreadPoolExecutorUtil.getThreadPoolExecutor().execute(() -> {
            this.saveFollowUpOperationLog(finalFollowUpRoute, finalOperationType);
        });
        return followUpRoute;
    }

    /**
     * @Author:FangZhiyi
     * @Description 删除随访路径
     * @Params [deleteFollowUpReq]
     * @Return org.atag.core.base.BaseResponse
     * @Data Created in 2020/7/28 15:05
     * @Moddified By:
     */
    @Override
    @RpcService
    public void deleteFollowUpRoute(FollowUpBaseReq deleteFollowUpReq) throws ControllerException {
        Long routeId = deleteFollowUpReq.getRouteId();
        String followType = deleteFollowUpReq.getFollowType();
        if (CommonUtils.isEmpty(routeId)) {
            throw new ControllerException("路径id不可为空");
        }
        if (CommonUtils.isEmpty(followType)) {
            throw new ControllerException("随访路径类型不可为空");
        }
        FollowUpRoute followUpRoute = null;
        byte operationType = 0;
        try {
            followUpRoute = followUpRouteDAO.get(routeId);
            if (followUpRoute == null) {
                throw new ControllerException("查无此记录，无法修改，请查证。");
            }
            if (deleteFollowUpReq.getOperateType() == 1) {//停用
                followUpRoute.setStatus((byte) 0);//0未启用
                operationType = 3;
            } else if (deleteFollowUpReq.getOperateType() == 2) {//删除
                //TODO 根据随访类型判断是否已经生成了计划
                Long count = 0L;
                if (FollowTypeEnum.FUS_SPECIALIST.getType().equals(followType)) {
                    count = followUpRecordsDAO.getRecordsCount(routeId);
                } else if (FollowTypeEnum.FUS_AFTER.getType().equals(followType)) {
                    count = fusAfterHosRecordDAO.getRecordsCount(routeId);
                } else if (FollowTypeEnum.FUS_SATISFACTION.getType().equals(followType)) {
                    count = patientSatisfactionSurveyDAO.getRecordsCount(routeId);
                }
                if (count > 0) {
                    throw new ControllerException(ResultCode.METHOD_FORBID, "该路径已使用，存在相关业务数据，不允许删除");
                }
                followUpRoute.setStatus((byte) 9);//9删除
                operationType = 4;
            }
            followUpRoute.setModifyDt(new Date());
            followUpRouteDAO.update(followUpRoute);
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ControllerException("请求失败" + e.getMessage());
        }
        /*保存操作日志*/
        FollowUpRoute finalFollowUpRoute = followUpRoute;
        byte finalOperationType = operationType;
        ThreadPoolExecutorUtil.getThreadPoolExecutor().execute(() -> {
            this.saveFollowUpOperationLog(finalFollowUpRoute, finalOperationType);
        });
        return;
    }

    /**
     * @Author:FangZhiyi
     * @Description 获取随访路径详情
     * @Params [followUpReq]
     * @Return org.atag.core.base.BaseResponse
     * @Data Created in 2020/7/28 15:07
     * @Moddified By:
     */
    @Override
    @RpcService
    public List<FollowUpRouteDetailResp> queryFollowUpRouteDetail(FollowUpBaseReq followUpReq) throws ControllerException {
        List<FollowUpRouteDetailResp> followUpRouteDetailRespList = null;
        try {
            followUpRouteDetailRespList = new ArrayList<>();
            List<FollowUpRouteDetail> followUpRouteDetailList = followUpRouteDetailDAO.queryFollowUpRouteDetailList(followUpReq.getOrganizationId(), followUpReq.getRouteId());
            if (CollectionUtils.isEmpty(followUpRouteDetailList)) {
                return followUpRouteDetailRespList;
            }
            //1.查询随访内容
            for (FollowUpRouteDetail followUpRouteDetail : followUpRouteDetailList) {
                FollowUpRouteDetailResp followUpRouteDetailResp = new FollowUpRouteDetailResp();
                BeanUtils.copyProperties(followUpRouteDetail, followUpRouteDetailResp);
                List<FollowUpRouteDetailContent> followUpList = followUpRouteDetailContentDAO.queryFollowUpList
                        (followUpReq.getOrganizationId(), followUpReq.getRouteId(), followUpRouteDetail.getRouteDetailId());//随访内容
                if (!CollectionUtils.isEmpty(followUpList)) {
                    followUpRouteDetailResp.setFollowUpList(followUpList);//随访内容
                }
                //2.1.因素模块，先查询因素主表信息
                List<FollowUpRouteDetailFactor> startFactorList = followUpRouteDetailFactorDAO.queryFactorListByFactorFlag
                        (followUpReq.getOrganizationId(), followUpReq.getRouteId(), followUpRouteDetail.getRouteDetailId(), (byte) 1);//随访开始因素
                List<FollowUpRouteDetailFactorRelationReq> startFactorRelationList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(startFactorList)) {
                    //2.2.查询开始因素详细信息
                    for (FollowUpRouteDetailFactor startFollow : startFactorList) {
                        FollowUpRouteDetailFactorRelationReq followRelationReq = new FollowUpRouteDetailFactorRelationReq();
                        BeanUtils.copyProperties(startFollow, followRelationReq);
                        List<FollowUpRouteDetailFactorRelation> followUpRouteDetailFactorRelations = followUpRouteDetailFactorRelationDAO.queryFactorRelationByRouteDetailFactorId(followRelationReq.getRouteDetailFactorId(), followUpRouteDetail.getRouteDetailId());
                        followRelationReq.setFollowUpFactorRelationList(followUpRouteDetailFactorRelations);
                        startFactorRelationList.add(followRelationReq);
                    }
                    followUpRouteDetailResp.setStartFactorList(startFactorRelationList);//随访开始因素
                }
                //2.3.查询停止因素主表信息
                List<FollowUpRouteDetailFactor> finishFactorList = followUpRouteDetailFactorDAO.queryFactorListByFactorFlag
                        (followUpReq.getOrganizationId(), followUpReq.getRouteId(), followUpRouteDetail.getRouteDetailId(), (byte) 2);//随访停止因素
                List<FollowUpRouteDetailFactorRelationReq> finishFactorRelationList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(finishFactorList)) {
                    //2.2.查询停止因素详细信息
                    for (FollowUpRouteDetailFactor finishFollow : finishFactorList) {
                        FollowUpRouteDetailFactorRelationReq followRelationReq = new FollowUpRouteDetailFactorRelationReq();
                        BeanUtils.copyProperties(finishFollow, followRelationReq);
                        List<FollowUpRouteDetailFactorRelation> followUpRouteDetailFactorRelations = followUpRouteDetailFactorRelationDAO.queryFactorRelationByRouteDetailFactorId(followRelationReq.getRouteDetailFactorId(), followUpRouteDetail.getRouteDetailId());
                        followRelationReq.setFollowUpFactorRelationList(followUpRouteDetailFactorRelations);
                        finishFactorRelationList.add(followRelationReq);
                    }
                    followUpRouteDetailResp.setFinishFactorList(finishFactorRelationList);//随访停止因素
                }
                //TODO 这里修改了推荐科室信息的保存位置，从之前的因素表迁移到了随访路径详情表单关联表 2022-05-31 lanyu
                //4.1.随访表单信息查询、门诊复诊推荐科室信息查询
                List<FollowUpRouteDetailForm> followUpRouteDetailForms =
                        followUpRouteDetailFormDAO.queryByRouteDetailId(followUpRouteDetail.getRouteDetailId());
                if (!CollectionUtils.isEmpty(followUpRouteDetailForms)) {
                    followUpRouteDetailResp.setFollowUpRouteDetailForms(followUpRouteDetailForms);
                }
                followUpRouteDetailRespList.add(followUpRouteDetailResp);
            }
        } catch (BeansException e) {
            e.printStackTrace();
            throw new ControllerException("请求失败" + e.getMessage());
        }
        return followUpRouteDetailRespList;
    }

    /**
     * @Author:FangZhiyi
     * @Description 保存随访路径详情
     * @Params [saveFollowUpRouteDetail]
     * @Return org.atag.core.base.BaseResponse
     * @Data Created in 2020/7/28 15:08
     * @Moddified By:AbstractNettyServerHandler
     */
    @Override
    @RpcService
    public void saveFollowUpRouteDetail(FollowUpRouteDetailResp saveFollowUpRouteDetail) throws ControllerException {
        byte operationType = 0;
        try {
            FollowUpRouteDetail followUpRouteDetail = new FollowUpRouteDetail();
            if (saveFollowUpRouteDetail.getRouteDetailId() == null) {//新增
                BeanUtils.copyProperties(saveFollowUpRouteDetail, followUpRouteDetail);
                followUpRouteDetail.setGmtCreate(new Date());
                followUpRouteDetailDAO.save(followUpRouteDetail);
                /*保存操作日志*/
                operationType = 5;//新增随访路径详情
                FollowUpRouteDetail finalFollowUpRouteDetail = followUpRouteDetail;
                byte finalOperationType = operationType;
                ThreadPoolExecutorUtil.getThreadPoolExecutor().execute(() -> {
                    this.saveFollowUpDetailOperationLog(finalFollowUpRouteDetail, finalOperationType);
                });
                /*随访内容详情*/
                List<FollowUpRouteDetailContent> followUpList = saveFollowUpRouteDetail.getFollowUpList();
                if (!CollectionUtils.isEmpty(followUpList)) {
                    saveContent(followUpList, followUpRouteDetail.getOrganizationId(), followUpRouteDetail.getRouteId(), followUpRouteDetail.getRouteDetailId());
                }
                /*开始因素*/
                List<FollowUpRouteDetailFactorRelationReq> startFactorList = saveFollowUpRouteDetail.getStartFactorList();
                if (!CollectionUtils.isEmpty(startFactorList)) {
                    saveFactorList(startFactorList, followUpRouteDetail.getOrganizationId(), followUpRouteDetail.getRouteId(), followUpRouteDetail.getRouteDetailId(), (byte) 1);
                }
                /*停止因素*/
                List<FollowUpRouteDetailFactorRelationReq> finishFactorList = saveFollowUpRouteDetail.getFinishFactorList();
                if (!CollectionUtils.isEmpty(finishFactorList)) {
                    saveFactorList(finishFactorList, followUpRouteDetail.getOrganizationId(), followUpRouteDetail.getRouteId(), followUpRouteDetail.getRouteDetailId(), (byte) 2);
                }
                /*表单/健康宣教/推荐科室信息*/
                List<FollowUpRouteDetailForm> followUpRouteDetailForms = saveFollowUpRouteDetail.getFollowUpRouteDetailForms();
                if (!CollectionUtils.isEmpty(followUpRouteDetailForms)) {
                    saveFollowUpRouteDetailForm(followUpRouteDetailForms, followUpRouteDetail.getOrganizationId(), followUpRouteDetail.getRouteId(), followUpRouteDetail.getRouteDetailId());
                }
            } else {//修改
                followUpRouteDetail = followUpRouteDetailDAO.get(saveFollowUpRouteDetail.getRouteDetailId());
                if (followUpRouteDetail == null) {
                    throw new ControllerException("查无此记录，无法修改，请查证。");
                }
                BeanUtils.copyProperties(saveFollowUpRouteDetail, followUpRouteDetail);
                followUpRouteDetail.setGmtModified(new Date());
                followUpRouteDetailDAO.update(followUpRouteDetail);
                operationType = 6;//修改随访路径详情
                FollowUpRouteDetail finalFollowUpRouteDetail = followUpRouteDetail;
                byte finalOperationType = operationType;
                ThreadPoolExecutorUtil.getThreadPoolExecutor().execute(() -> {
                    this.saveFollowUpDetailOperationLog(finalFollowUpRouteDetail, finalOperationType);
                });
                List<FollowUpRouteDetailContent> followUpList = saveFollowUpRouteDetail.getFollowUpList();
                /*因为会更改类型，有些类型没有随访内容，所以不管有没有传都先进行删除*/
                followUpRouteDetailContentDAO.deleteContentByRouteDetailId(followUpRouteDetail.getRouteDetailId());//先刪除再新增
                if (!CollectionUtils.isEmpty(followUpList)) {
                    saveContent(followUpList, followUpRouteDetail.getOrganizationId(), followUpRouteDetail.getRouteId(), followUpRouteDetail.getRouteDetailId());
                }
                /*开始因素*/
                List<FollowUpRouteDetailFactorRelationReq> startFactorList = saveFollowUpRouteDetail.getStartFactorList();
                if (!CollectionUtils.isEmpty(startFactorList)) {
                    //删除信息
                    deleteFactorList(startFactorList, followUpRouteDetail.getRouteDetailId(), FactorFlagEnum.START.getType());
                    saveFactorList(startFactorList, followUpRouteDetail.getOrganizationId(), followUpRouteDetail.getRouteId(), followUpRouteDetail.getRouteDetailId(), (byte) 1);
                } else {
                    //删除信息
                    deleteFactorList(startFactorList, followUpRouteDetail.getRouteDetailId(), FactorFlagEnum.START.getType());
                }
                /*停止因素*/
                List<FollowUpRouteDetailFactorRelationReq> finishFactorList = saveFollowUpRouteDetail.getFinishFactorList();
                if (!CollectionUtils.isEmpty(finishFactorList)) {
                    //删除信息
                    deleteFactorList(finishFactorList, followUpRouteDetail.getRouteDetailId(), FactorFlagEnum.STOP.getType());
                    saveFactorList(finishFactorList, followUpRouteDetail.getOrganizationId(), followUpRouteDetail.getRouteId(), followUpRouteDetail.getRouteDetailId(), (byte) 2);
                } else {
                    //删除信息
                    deleteFactorList(finishFactorList, followUpRouteDetail.getRouteDetailId(), FactorFlagEnum.STOP.getType());
                }
                /*表单/健康宣教/推荐科室信息*/
                List<FollowUpRouteDetailForm> followUpRouteDetailForms = saveFollowUpRouteDetail.getFollowUpRouteDetailForms();
                if (!CollectionUtils.isEmpty(followUpRouteDetailForms)) {
                    followUpRouteDetailFormDAO.deleteFollowUpDetailFormByRouteDetailId(followUpRouteDetail.getRouteDetailId());
                    saveFollowUpRouteDetailForm(followUpRouteDetailForms, followUpRouteDetail.getOrganizationId(), followUpRouteDetail.getRouteId(), followUpRouteDetail.getRouteDetailId());
                }
            }
        } catch (BeansException e) {
            e.printStackTrace();
            throw new ControllerException("请求失败" + e.getMessage());
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ControllerException("请求失败" + e.getMessage());
        }
        return;
    }


    /**
     * @Author:FangZhiyi
     * @Description 删除随访路径详情
     * @Params [deleteDetailReq]
     * @Return org.atag.core.base.BaseResponse
     * @Data Created in 2020/7/28 15:08
     * @Moddified By:
     */
    @Override
    @RpcService
    public void deleteFollowUpRouteDetail(FollowUpBaseReq deleteDetailReq) throws ControllerException {
        try {
            FollowUpRouteDetail followUpRouteDetail = followUpRouteDetailDAO.get(deleteDetailReq.getRouteDetailId());
            if (followUpRouteDetail == null) {
                throw new ControllerException("查无此记录，无法修改，请查证。");
            }
            followUpRouteDetailDAO.remove(deleteDetailReq.getRouteDetailId());
            FollowUpRouteDetail finalFollowUpRouteDetail = followUpRouteDetail;
            ThreadPoolExecutorUtil.getThreadPoolExecutor().execute(() -> {
                this.saveFollowUpDetailOperationLog(finalFollowUpRouteDetail, (byte) 8);
            });
            followUpRouteDetailContentDAO.deleteContentByRouteDetailId(deleteDetailReq.getRouteDetailId());
            followUpRouteDetailFactorDAO.removeFactorByDetailId(deleteDetailReq.getRouteDetailId());
            followUpRouteDetailFactorRelationDAO.deleteFactorRelationByRouteDetailId(deleteDetailReq.getRouteDetailId());
            followUpRouteDetailFormDAO.deleteFollowUpDetailFormByRouteDetailId(deleteDetailReq.getRouteDetailId());
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ControllerException("请求失败" + e.getMessage());
        }
        return;
    }


    /**
     * @Author:FangZhiyi
     * @Description 复制随访路径详细
     * @Params [copyDetailReq]
     * @Return org.atag.core.base.BaseResponse
     * @Data Created in 2020/7/28 15:08
     * @Moddified By:
     */
    @Override
    @RpcService
    public void copyFollowUpRouteDetail(FollowUpBaseReq copyDetailReq) throws ControllerException {
        try {
            FollowUpRouteDetail followUpRouteDetail = followUpRouteDetailDAO.get(copyDetailReq.getRouteDetailId());
            if (followUpRouteDetail == null) {
                throw new ControllerException("查无此记录，无法修改，请查证。");
            }
            followUpRouteDetail.setRouteDetailId(null);
            followUpRouteDetail.setGmtCreate(new Date());
            //更新阶段信息 3-1 -> 4-1
            followUpRouteDetail.setFollowUpStageFirstSeq(followUpRouteDetail.getFollowUpStageFirstSeq() + 1);
            followUpRouteDetailDAO.save(followUpRouteDetail);
            FollowUpRouteDetail finalFollowUpRouteDetail = followUpRouteDetail;
            ThreadPoolExecutorUtil.getThreadPoolExecutor().execute(() -> {
                this.saveFollowUpDetailOperationLog(finalFollowUpRouteDetail, (byte) 7);
            });
            /*表单详情内容*/
            List<FollowUpRouteDetailContent> followUpList = followUpRouteDetailContentDAO.queryFollowUpList(copyDetailReq.getOrganizationId(),
                    copyDetailReq.getRouteId(), copyDetailReq.getRouteDetailId());
            if (!CollectionUtils.isEmpty(followUpList)) {
                saveContent(followUpList, followUpRouteDetail.getOrganizationId(), followUpRouteDetail.getRouteId(), followUpRouteDetail.getRouteDetailId());
            }
            /*开始停止结束因素*/
            List<FollowUpRouteDetailFactor> startAndFinishFactorList = followUpRouteDetailFactorDAO.queryFactorList(copyDetailReq.getOrganizationId(), copyDetailReq.getRouteId(), copyDetailReq.getRouteDetailId());
            if (!CollectionUtils.isEmpty(startAndFinishFactorList)) {
                List<FollowUpRouteDetailFactorRelationReq> finishFactorRelationList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(startAndFinishFactorList)) {
                    //2.2.查询因素详细信息
                    for (FollowUpRouteDetailFactor finishFollow : startAndFinishFactorList) {
                        FollowUpRouteDetailFactorRelationReq followRelationReq = new FollowUpRouteDetailFactorRelationReq();
                        BeanUtils.copyProperties(finishFollow, followRelationReq);
                        List<FollowUpRouteDetailFactorRelation> followUpRouteDetailFactorRelations =
                                followUpRouteDetailFactorRelationDAO.queryFactorRelationByRouteDetailFactorId(followRelationReq.getRouteDetailFactorId(), copyDetailReq.getRouteDetailId());
                        followRelationReq.setFollowUpFactorRelationList(followUpRouteDetailFactorRelations);
                        finishFactorRelationList.add(followRelationReq);
                    }
                }
                saveFactorList(finishFactorRelationList, followUpRouteDetail.getOrganizationId(), followUpRouteDetail.getRouteId(), followUpRouteDetail.getRouteDetailId(), null);
            }
            /*随访表单/健康宣教/推荐科室关联*/
            List<FollowUpRouteDetailForm> followUpRouteDetailForms = followUpRouteDetailFormDAO.queryByRouteDetailId(copyDetailReq.getRouteDetailId());
            if (!CollectionUtils.isEmpty(followUpRouteDetailForms)) {
                saveFollowUpRouteDetailForm(followUpRouteDetailForms, followUpRouteDetail.getOrganizationId(), followUpRouteDetail.getRouteId(), followUpRouteDetail.getRouteDetailId());
            }
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ControllerException("复制失败" + e.getMessage());
        }
        return;
    }


    /**
     * 随访路径启禁用
     *
     * @param followUpBaseReq
     * @throws ControllerException
     */
    @Override
    public void enableOrDisableRoute(FollowUpBaseReq followUpBaseReq) throws ControllerException {
        if (CommonUtils.isEmpty(followUpBaseReq.getRouteId())) {
            throw new ControllerException("路径id不可为空");
        }
        //启用禁用路径
        followUpRouteDAO.updateEnableOrDisableRoute(followUpBaseReq.getRouteId(), followUpBaseReq.getStatus());
    }

    /**
     * 审核状态更改
     *
     * @param req the req  getDiseasesAuditStatus
     * @author Qulc - 2022/6/6 15:05
     */
    @Override
    @RpcService
    public void updateAuditStatus(UpdateAfterAuditStatusQo req) throws ControllerException {
        fusAfterHosRecordDAO.updateAuditStatus(req.getAuditStatus(), req.getRecordIdList(), req.getAuditName());
    }

    /**
     * 专病审核状态更改
     *
     * @param req the req
     * @author Qulc - 2022/6/6 16:51
     */
    @Override
    @RpcService
    public void updateDiseasesAuditStatus(UpdateAuditStatusQo req) throws ControllerException {
        followUpRecordsDAO.updateAuditStatus(req.getAuditStatus().byteValue(), req.getRecordIdList(), req.getAuditName());
    }


    /**
     * 院后随访列表
     *
     * @param qo the qo
     * @author Qulc - 2022/6/2 9:20
     */
    @Override
    @RpcService
    public SimplePageInfo<FusAfterHosRecordDTO> getAfterHosRecord(QueryAfterHosRecordQo qo) throws ControllerException {
        List<Long> routeIdList = null;
        if (CommonUtils.isNotEmpty(qo.getDocId())) {
            List<Long> teamIdList = docTeamRpcService.queryTeamIdListByDocId(qo.getDocId());
            routeIdList = followUpRouteDAO.queryRouteIdList("1", qo.getDocId(),teamIdList);
            if (CommonUtils.isEmpty(routeIdList)) {
                return new SimplePageInfo<FusAfterHosRecordDTO>();
            }
            qo.setRouteIdList(routeIdList);

        }

        SimplePageInfo<FusAfterHosRecordDTO> afterHosRecord = followUpRouteDAO.getAfterHosRecord(qo);
        if (CommonUtils.isNotEmpty(afterHosRecord.getList())) {
            for (FusAfterHosRecordDTO fusAfterHosRecordDTO : afterHosRecord.getList()) {
                //随访阶段名称赋值
                FollowUpRouteDetail followUpRouteDetail = followUpRouteDetailDAO.getByRouteDetailId(fusAfterHosRecordDTO.getRouteDetailId());
                fusAfterHosRecordDTO.setFollowUpStageName(followUpRouteDetail.getFollowUpStageName());
                //路径详细类型中文
                fusAfterHosRecordDTO.setRouteDetailTypeText(RouteDetailTypeEnum.getNameByKey(fusAfterHosRecordDTO.getRouteDetailType().toString()));


                //门诊
                if ("1".equals(fusAfterHosRecordDTO.getPatientType())) {
                    //入组方式赋值
                    VrOutpatientVisitRecord vrOutpatientVisitRecord = vrOutpatientVisitRecordDAO.getByVisitNo(fusAfterHosRecordDTO.getVisitNo(), fusAfterHosRecordDTO.getOrganizationId());
                    if (null != vrOutpatientVisitRecord) {
                        fusAfterHosRecordDTO.setMainDiagnosisName(vrOutpatientVisitRecord.getMainDiagnosisName());
                        //端那边显示的是病案号，都用这个字段显示
                        fusAfterHosRecordDTO.setMedicalRecordNumber(vrOutpatientVisitRecord.getPatientMedicalCardNumber());
                    }
                    //住院
                } else if ("2".equals(fusAfterHosRecordDTO.getPatientType())) {
                    //入组方式赋值
                    VrInpatientVisitRecord vrInpatientVisitRecord = vrInpatientVisitRecordDAO.getVrInpatientVisitRecord(fusAfterHosRecordDTO.getVisitNo(), fusAfterHosRecordDTO.getOrganizationId());
                    if (null != vrInpatientVisitRecord) {
                        fusAfterHosRecordDTO.setMainDiagnosisName(vrInpatientVisitRecord.getDischargeDiagnosis());
                    }
                }

                //患者标签赋值
                List<FollowPatientLabelEntity> fplByVisitNo = followPatientLabelDAO.findFplByVisitNo(Integer.valueOf(fusAfterHosRecordDTO.getPatientType()), fusAfterHosRecordDTO.getVisitNo());
                if (CommonUtils.isNotEmpty(fplByVisitNo)) {
                    //从字典查询患者标签中文
                    Map<String, Object> objectMap = baseDictionaryService.queryDicFileCode("hcn.ods.followup.patientLabe");
                    StringBuffer stringBuffer = new StringBuffer();
                    if (null != objectMap) {
                        for (FollowPatientLabelEntity followPatientLabelEntity : fplByVisitNo) {
                            String text = (String) objectMap.get(followPatientLabelEntity.getDisputeType());
                            if (CommonUtils.isNotEmpty(text)) {
                                stringBuffer.append(",").append(text);
                            }
                        }
                    }

                    if (stringBuffer.length() > 0) {
                        fusAfterHosRecordDTO.setPatientLabelText(stringBuffer.substring(1));
                    }

                }
            }
        }
        return afterHosRecord;
    }


    /**
     * 专病随访列表
     *
     * @param qo the qo
     * @author Qulc - 2022/6/6 14:25
     */
    @Override
    @RpcService
    public SimplePageInfo<FollowUpRecordsVO> getFollowUpRecords(DocIdQo qo) throws ControllerException {
        Long routeId = qo.getRouteId();
        List<Long> routeIdList = null;
        if (CommonUtils.isNotEmpty(qo.getDocId())) {
            List<Long> teamIdList = docTeamRpcService.queryTeamIdListByDocId(qo.getDocId());
            routeIdList = followUpRouteDAO.queryRouteIdList("2", qo.getDocId(),teamIdList);
            List<Long> list = routeIdList.stream().filter(s -> s.equals(routeId)).collect(Collectors.toList());
            if (CommonUtils.isEmpty(routeIdList)) {
                return new SimplePageInfo<FollowUpRecordsVO>();
            }
            qo.setRouteIdList(list);
        }

        SimplePageInfo<FollowUpRecordsVO> afterHosRecord = followUpRouteDAO.getFollowUpRecords(qo);
        if (CommonUtils.isNotEmpty(afterHosRecord.getList())) {
            for (FollowUpRecordsVO followUpRecordsVO : afterHosRecord.getList()) {
                //随访阶段名称赋值
                FollowUpRouteDetail followUpRouteDetail = followUpRouteDetailDAO.getByRouteDetailId(followUpRecordsVO.getRouteDetailId());
                followUpRecordsVO.setFollowUpStageName(followUpRouteDetail.getFollowUpStageName());

                //入组方式赋值
                followUpRecordsVO.setApplyTypeText(ApplyTypeEnum.getNameByKey(followUpRecordsVO.getApplyType()));

            }
        }
        return afterHosRecord;
    }


    /**
     * 区分是否是数字与字符串
     *
     * @param number the number
     * @author Qulc - 2022/6/6 15:07
     */
    private boolean isNumber(String number) {
        if (StringUtils.isEmpty(number)) {
            return false;
        }
        Pattern p = Pattern.compile("[0-9]+");
        Matcher m = p.matcher(number);
        return m.find();
    }

    /**
     * 随访计划审核---院后随访机构路径树查询
     *
     * @param qo the qo
     * @author Qulc - 2022/6/1 9:37
     */
    @Override
    @RpcService
    public FollowUpTreeListVO getRouteTreeList(DocIdQo qo) throws ControllerException {
        if(CommonUtils.isNotEmpty(qo.getDocId())){
            List<Long> teamIdList = docTeamRpcService.queryTeamIdListByDocId(qo.getDocId());
            qo.setTeamIdList(teamIdList);
        }
        List<FollowUpCountVo> List = followUpRouteDAO.getRouteTreeList(qo);
        if (CommonUtils.isEmpty(List)) {
            return null;
        }

        //查询路径下的需要审核的随访计划数量
        Map planCountMap = followUpRouteDAO.queryPlanCount("1");


        Map<String, List<FollowUpCountVo>> map = new HashMap<>();
        for (FollowUpCountVo treeList : List) {
            Integer count = planCountMap.get(treeList.getRouteId()) == null ? 0 : (Integer) planCountMap.get(treeList.getRouteId());
            treeList.setPlanCount(count);

            String organizationId = treeList.getOrganizationId();
            List<FollowUpCountVo> followUpRouteList = map.get(organizationId);//返回指定键所映射的值
            if (CommonUtils.isNotEmpty(followUpRouteList)) {
                followUpRouteList.add(treeList);
            } else {
                List<FollowUpCountVo> roteList = new ArrayList<>();
                roteList.add(treeList);
                map.put(organizationId, roteList);
            }
        }

        FollowUpTreeListVO vo = new FollowUpTreeListVO();
        List<FollowUpTreeListVO.Org> orgList = new ArrayList<>();
        Set<Map.Entry<String, List<FollowUpCountVo>>> entries = map.entrySet();

        for (Map.Entry<String, List<FollowUpCountVo>> entry : entries) {
            String organizationId = entry.getKey();//键
            List<FollowUpCountVo> routeList = entry.getValue();//值
            FollowUpTreeListVO.Org org = new FollowUpTreeListVO.Org();//机构对象
            org.setOrganizationId(organizationId);//把从map里拿到的机构键放到org
            //todo
            List<FollowUpTreeListVO.Route> routeList2 = new ArrayList<>();//二级随访路径
            for (FollowUpCountVo rout : routeList) {
                FollowUpTreeListVO.Route thank = new FollowUpTreeListVO.Route();
                BeanUtils.copyProperties(rout, thank);
                routeList2.add(thank);
            }

            org.setRouteList(routeList2);
            org.setOrganizationName(routeList.get(0).getOrganizationName());
            orgList.add(org);
            vo.setOrgList(orgList);
        }

        return vo;
    }


    /**
     * 随访计划审核---专病随访机构路径树查询
     *
     * @param qo the qo
     * @author Qulc - 2022/6/6 9:55
     */
    @Override
    @RpcService
    public FollowUpTreeListVO getDiseaseRouteTreeList(DocIdQo qo) throws ControllerException {
        if(CommonUtils.isNotEmpty(qo.getDocId())){
            List<Long> teamIdList = docTeamRpcService.queryTeamIdListByDocId(qo.getDocId());
            qo.setTeamIdList(teamIdList);
        }
        List<FollowUpCountVo> List = followUpRouteDAO.getDiseaseRouteTreeList(qo);
        if (CommonUtils.isEmpty(List)) {
            return null;
        }

        //查询路径下的需要审核的随访计划数量
        Map planCountMap = followUpRouteDAO.queryPlanCount("2");

        Map<String, List<FollowUpCountVo>> map = new HashMap<>();
        for (FollowUpCountVo treeList : List) {
            Integer count = planCountMap.get(treeList.getRouteId()) == null ? 0 : (Integer) planCountMap.get(treeList.getRouteId());
            treeList.setPlanCount(count);

            String organizationId = treeList.getOrganizationId();
            List<FollowUpCountVo> followUpRouteList = map.get(organizationId);//返回指定键所映射的值
            if (CommonUtils.isNotEmpty(followUpRouteList)) {
                followUpRouteList.add(treeList);
            } else {
                List<FollowUpCountVo> roteList = new ArrayList<>();
                roteList.add(treeList);
                map.put(organizationId, roteList);
            }
        }

        FollowUpTreeListVO vo = new FollowUpTreeListVO();
        List<FollowUpTreeListVO.Org> orgList = new ArrayList<>();
        Set<Map.Entry<String, List<FollowUpCountVo>>> entries = map.entrySet();

        for (Map.Entry<String, List<FollowUpCountVo>> entry : entries) {
            String organizationId = entry.getKey();//键
            List<FollowUpCountVo> routeList = entry.getValue();//值
            FollowUpTreeListVO.Org org = new FollowUpTreeListVO.Org();//机构对象
            org.setOrganizationId(organizationId);//把从map里拿到的机构键放到org
            Organization orgDetailByOrgId = organizationService.getOrgDetailByOrgCode(organizationId);
            if (Objects.nonNull(orgDetailByOrgId)) {
                org.setOrganizationName(orgDetailByOrgId.getShortName());
            }
            List<FollowUpTreeListVO.Route> routeList2 = new ArrayList<>();//二级随访路径
            for (FollowUpCountVo rout : routeList) {
                FollowUpTreeListVO.Route thank = new FollowUpTreeListVO.Route();
                BeanUtils.copyProperties(rout, thank);
                routeList2.add(thank);
            }

            org.setRouteList(routeList2);
            orgList.add(org);
            vo.setOrgList(orgList);
        }

        return vo;
    }


    /*批量新增随访路径详情的因素*/
    private void saveFactorList(List<FollowUpRouteDetailFactorRelationReq> factorList, String orgId, Long routeId, Long routeDetailId, Byte factorFlag) {
        for (int i = 0; i < factorList.size(); i++) {
            FollowUpRouteDetailFactorRelationReq followUpRouteDetailFactor = factorList.get(i);
            //第一个因素条件如果没传默认为 and
            if (i == 0) {
                followUpRouteDetailFactor.setFactorConditionType(FactorConditionTypeEnum.AND.getType());
            }
            //1.保存开始结束因素的基本信息
            followUpRouteDetailFactor.setRouteDetailFactorId(null);
            followUpRouteDetailFactor.setOrganizationId(orgId);
            followUpRouteDetailFactor.setRouteId(routeId);
            followUpRouteDetailFactor.setRouteDetailId(routeDetailId);
            //设置sort字段
            followUpRouteDetailFactor.setSort(i + 1);
            if (factorFlag != null) {
                followUpRouteDetailFactor.setFactorFlag(factorFlag);
            }
            followUpRouteDetailFactor.setGmtCreate(new Date());
            FollowUpRouteDetailFactor followFactorDetail = new FollowUpRouteDetailFactor();
            BeanUtils.copyProperties(followUpRouteDetailFactor, followFactorDetail);
            FollowUpRouteDetailFactor save = followUpRouteDetailFactorDAO.save(followFactorDetail);
            //2.保存开始结束因素内容信息
            List<FollowUpRouteDetailFactorRelation> followUpFactorRelationList = followUpRouteDetailFactor.getFollowUpFactorRelationList();
            if (CommonUtils.isNotEmpty(followUpFactorRelationList)) {
                for (FollowUpRouteDetailFactorRelation follow : followUpFactorRelationList) {
                    follow.setRouteDetailId(routeDetailId);
                    follow.setRouteDetailFactorId(save.getRouteDetailFactorId());
                    follow.setFactorType(save.getFactorType());
                    follow.setCreateDt(new Date());
                    follow.setModifyDt(new Date());
                    followUpRouteDetailFactorRelationDAO.save(follow);
                }
            }
        }
    }

    /**
     * 删除随访路径详情因素
     *
     * @param factorList
     * @param routeDetailId
     * @param factorFlag
     */
    private void deleteFactorList(List<FollowUpRouteDetailFactorRelationReq> factorList, Long routeDetailId, Byte factorFlag) {
        //1.删除主表信息
        followUpRouteDetailFactorDAO.deleteContentByFactorDetailId(routeDetailId, factorFlag);
        //2.删除路径内容信息表
        for (FollowUpRouteDetailFactorRelationReq follow : factorList) {
            followUpRouteDetailFactorRelationDAO.deleteRouteDetailFactorRelationById(routeDetailId, follow.getRouteDetailFactorId());
        }
    }

    /*批量新增随访路径详情的内容*/
    private void saveContent(List<FollowUpRouteDetailContent> contentList, String orgId, Long routeId, Long routeDetailId) {
        for (FollowUpRouteDetailContent followUpRouteDetailContent : contentList) {
            followUpRouteDetailContent.setRouteDetailContentId(null);
            followUpRouteDetailContent.setOrganizationId(orgId);
            followUpRouteDetailContent.setRouteId(routeId);
            followUpRouteDetailContent.setRouteDetailId(routeDetailId);
            followUpRouteDetailContent.setGmtCreate(new Date());
            followUpRouteDetailContentDAO.save(followUpRouteDetailContent);
        }
    }

    /*保存随访路径详情表单关联表*/
    private void saveFollowUpRouteDetailForm(List<FollowUpRouteDetailForm> followUpRouteDetailForms, String orgId, Long routeId, Long routeDetailId) {
        for (FollowUpRouteDetailForm detailForm : followUpRouteDetailForms) {
            detailForm.setOrganizationId(orgId);
            detailForm.setRouteId(routeId);
            detailForm.setRouteDetailId(routeDetailId);
            followUpRouteDetailFormDAO.save(detailForm);
        }
    }

    /*保存随访路径操作日志*/
    private void saveFollowUpOperationLog(FollowUpRoute followUpRoute, byte operationType) {
        FollowUpOperationLog followUpOperationLog = new FollowUpOperationLog();
        followUpOperationLog.setOperationRouteId(followUpRoute.getRouteId());
        followUpOperationLog.setOperationType(operationType);
        StringBuffer stringBuffer = new StringBuffer();
        switch (operationType) {
            case 1://新增
                stringBuffer.append("新增");
                break;
            case 2://更新
                stringBuffer.append("更新");
                break;
            case 3://停用
                stringBuffer.append("停用");
                break;
            case 4://删除
                stringBuffer.append("删除");
                break;

        }
        stringBuffer.append("RouteId:").append(followUpRoute.getRouteId()).append(",名称为：").append(followUpRoute.getRouteName()).append("的随访路径");
        followUpOperationLog.setOperationContent(stringBuffer.toString());
        followUpOperationLog.setOperatorId(followUpRoute.getCreatorId());
        followUpOperationLog.setOperatorName(followUpRoute.getCreatorName());
        followUpOperationLog.setOperationTime(new Date());
        followUpOperationLogDAO.save(followUpOperationLog);
    }

    /*保存随访路径操作日志*/
    private void saveFollowUpDetailOperationLog(FollowUpRouteDetail followUpRouteDetail, byte operationType) {
        FollowUpOperationLog followUpOperationLog = new FollowUpOperationLog();
        followUpOperationLog.setOperationRouteId(followUpRouteDetail.getRouteId());
        followUpOperationLog.setOperationType(operationType);
        StringBuffer stringBuffer = new StringBuffer();
        switch (operationType) {
            case 5://随访路径详情新增
                stringBuffer.append("新增");
                break;
            case 6://随访路径详情更新
                stringBuffer.append("更新");
                break;
            case 7://随访路径详情复制
                stringBuffer.append("复制");
                break;
            case 8://随访路径详情删除
                stringBuffer.append("删除");
                break;

        }
        stringBuffer.append("RouteId:").append(followUpRouteDetail.getRouteId()).append(",RouteName为：").append(followUpRouteDetail.getRouteName())
                .append("RouteDetailId:").append(followUpRouteDetail.getRouteDetailId()).append(",路径详细类型为：")
                .append(followUpRouteDetail.getRouteDetailType()).append("的随访路径详情");
        followUpOperationLog.setOperationContent(stringBuffer.toString());
        followUpOperationLog.setOperatorId(null);
        followUpOperationLog.setOperatorName(null);
        followUpOperationLog.setOperationTime(new Date());
        followUpOperationLogDAO.save(followUpOperationLog);
    }


    /**
     * @Author:FangZhiYi
     * @Description 根据随访ID获取随访记录详情
     * @Params [recordId]
     * @Return specialist.entity.FollowUpRecords
     * @Data Created in 2021/12/9 17:11
     * @Moddified By:
     */
    @RpcService
    @Override
    public List<JSONObject> getFollowUpRecordsById(String patientId) throws ControllerException {
        List<JSONObject> resultList = new ArrayList<>();
        List<FollowUpRecords> followUpRecords = followUpRecordsDAO.queryFollowUpRecordsByPersonId(patientId);
        if (CommonUtils.isNotEmpty(followUpRecords)) {
            for (FollowUpRecords followUpRecord : followUpRecords) {
                resultList.add(JSONObject.parseObject(JSONObject.toJSONString(followUpRecord)));
            }
        }
        return resultList;
    }

    /**
     * 根据条件查询随访路径
     */
    @Override
    public List<FollowUpRouteVO> queryRouteList(FollowUpRouteQo qo){

        List<FollowUpRouteVO> list = followUpRouteDAO.queryRouteList(qo);
        if(!CollectionUtils.isEmpty(list) && qo.isShowManual()){
            FollowUpRouteVO vo = new FollowUpRouteVO();
            vo.setRouteId("0");
            vo.setRouteName("手动设置的随访路径");
            list.add(0, vo);
        }
        return list;
    }


    /**
     * 随访路径列表查询（新增）
     *
     * @param addDocTeamFusAuthorityReq the addDocTeamFusAuthorityReq
     * @author Qulc - 2022/5/23 16:48
     */
    @Override
    @RpcService
    public List<FollowUpRoute> queryFusList(AddDocTeamFusAuthorityReq addDocTeamFusAuthorityReq) throws ControllerException {
        List<FollowUpRoute> followUpRouteList = new ArrayList<>();
        if (CommonUtils.isNotEmpty(addDocTeamFusAuthorityReq.getOrgId()) || CommonUtils.isNotEmpty(addDocTeamFusAuthorityReq.getDeptId())) {
            String orgId = addDocTeamFusAuthorityReq.getOrgId();
            String deptId = addDocTeamFusAuthorityReq.getDeptId();
            String followType = addDocTeamFusAuthorityReq.getFollowType();
            followUpRouteList = followUpRouteDAO.queryFollowUpRouteListByOrdOrDept(orgId, deptId, followType);

        } else {
            throw new ControllerException("科室机构代码为空，请重新核对科室代码与机构代码数值，核对后按正确值输入");
        }
        return followUpRouteList;
    }

}
